package com.bj58.ecdata.dashboard.utils;

import java.text.DecimalFormat;

import org.apache.commons.lang.StringUtils;

public class VelocityTools {

	/**
	 * 百分比转换
	 */
	public double tranRate(double num) {
		DecimalFormat df = new DecimalFormat("0.00");
		if (num == 0) {
			return 0;
		}
		return Double.valueOf(df.format(num * 100));
	}

	// 四位小数，不以千分位分隔，百分比用
	public static String transTo4Point(double num) {
		DecimalFormat df = new DecimalFormat("0.0000");
		if (num == 0) {
			return "0";
		}
		return String.valueOf(Double.valueOf(df.format(num)));
	}

	// 计算占比 num1/num2
	public static String compRateDirect(Object num1, Object num2) {
		if (num1 == null || num2 == null) {
			return "--";
		}
		DecimalFormat df = new DecimalFormat("0.00");
		String strVal1 = String.valueOf(num1);
		String strVal2 = String.valueOf(num2);
		double num_one = Double.valueOf(strVal1);
		double num_two = Double.valueOf(strVal2);

		if (num_two == 0) {
			return "0";
		}
		double sss = (num_one) / num_two;
		if (sss > 0) {
			return df.format(sss * 100) + "%";
		} else {
			return "--";
		}

	}

	// 整型，千分位分隔
	public static String trans2Int(Object a) {
		if (null == a) {
			return "--";
		}
		DecimalFormat df3 = new DecimalFormat("#,###");
		return df3.format(Double.valueOf(String.valueOf(a)));
	}

	// 浮点，千分位分隔，两位小数
	public static String trans2Float(double num) {
		DecimalFormat df = new DecimalFormat("#,##0.00");
		if (num == 0) {
			return "0";
		}
		return df.format(num);
	}

	public boolean isSelected(String value, String value2) {
		if (StringUtils.equalsIgnoreCase(value, value2)) {
			return true;
		}
		return false;
	}

	/**
	 * 环比计算 (num1- num2)/num2
	 * @return
	 */
	public static String compRatio(Object num1,Object num2){
		if(num1==null && num2==null)
    		return "0.00";
    	else if(num1==null)
    		return  "-100.00";
    	else if(num2==null)
    		return  "100.00";
		String strVal1=String.valueOf(num1);
		String strVal2=String.valueOf(num2);
		
		DecimalFormat df = new DecimalFormat("0.00");
		double num_one = Double.valueOf(strVal1);
		double num_two = Double.valueOf(strVal2);
		
		if(num_one==0 && num_two == 0){
			return  "0.00";
		}else if(num_two == 0){
			return  "100.00";
		}else if(num_one == 0){
			return  "-100.00";
		}
		
		double sss = (num_one-num_two)/num_two;
		return  df.format(sss*100);
	}
	
}
